package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.UserLocation;
import com.itheima.service.mongo.UserLocationService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Guanxing
 * @date 2021/9/25
 */
@Service
public class UserLocationServiceImpl implements UserLocationService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveOrUpdateUserLocation(Double latitude, Double longitude, String addrStr, Long userId) {
        //查询用户坐标是否存在
        Query query = new Query(
                Criteria.where("userId").gte(userId)
        );
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        if (userLocation == null) {
            userLocation = new UserLocation();
            //执行保存
            userLocation.setUserId(userId);
            userLocation.setAddress(addrStr);
            userLocation.setCreated(System.currentTimeMillis());
            userLocation.setUpdated(System.currentTimeMillis());
            userLocation.setLastUpdated(System.currentTimeMillis());
            //坐标
            GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude,latitude);

            userLocation.setLocation(geoJsonPoint);
            mongoTemplate.save(userLocation);
        }else {
            //执行修改
            userLocation.setAddress(addrStr);
            userLocation.setLastUpdated(userLocation.getUpdated());
            userLocation.setUpdated(System.currentTimeMillis());
            //坐标
            GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude,latitude);
            userLocation.setLocation(geoJsonPoint);
            mongoTemplate.save(userLocation);
        }
    }

    //附近的人
    @Override
    public List<Long> findNearUserIdList(Long userId, Long distance) {
        //查询我的位置
        Query query1 = new Query(
                Criteria.where("userId").is(userId)
        );
        UserLocation myLocation = mongoTemplate.findOne(query1, UserLocation.class);

        //确定半径
        Distance dis = new Distance(distance / 1000, Metrics.KILOMETERS);

        //画圈
        Circle circle = new Circle(myLocation.getLocation(),dis);

        //搜索附近人坐标
        Query query2 = new Query(
                Criteria.where("location").withinSphere(circle)
        );
        List<UserLocation> userLocationList = mongoTemplate.find(query2, UserLocation.class);

        //获取圆中用户id
        List<Long> userIdList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userLocationList)) {
            for (UserLocation userLocation : userLocationList) {
                userIdList.add(userLocation.getUserId());
            }
        }
        return userIdList;
    }

    //根据用户id获取用户位置的坐标值
    @Override
    public List<Double> findBaiDuMap(String id) {
        Query query = new Query(
                Criteria.where("userId").is(Long.valueOf(id))
        );
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        return userLocation.getLocation().getCoordinates();
    }
}
